package com.ctg.itrdc.sysmgr.portal.control;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.sysmgr.portal.api.dto.CommonRegionDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ICommonRegionService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.data.entity.CommonRegion;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;

@Controller
@RequestMapping("/common_region")
public class CommonRegionController extends SysmgrCURDController<Long, CommonRegionDTO> {

	private static final String Q_FLAG_QUERY = "query";
	@Resource
	private ICommonRegionService commonRegionService;

	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid CommonRegionDTO dto, BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		long i = commonRegionService.insert(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增数据成功", i);
	}

	// @RequestMapping("/tree")
	// @ResponseBody
	// public CommonResult selectTree(){
	// List<CommonRegionDTO> dtoList = commonRegionService.selectRegionTree();
	// return new CommonResult(HttpStatus.OK.toString(), "",dtoList);
	// }

	/**
	 * @throws Exception
	 * @api {get} /common_region/query/editData 查询编辑数据
	 * @apiGroup common_region
	 * @apiName editData
	 * @apiParam {Long} commonRegionId
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/query/editData")
	@ResponseBody
	public CommonResult selectEditData(@RequestParam(required = false) String qFlag, CommonRegionDTO dto)
			throws Exception {
		if (Q_FLAG_QUERY.equalsIgnoreCase(qFlag)) {
			return this.selectByExample(dto);
		}
		Map<String, Map<String, Object>> data = commonRegionService.selectEditData(dto);
		return new CommonResult(HttpStatus.OK.toString(), "查询数据成功", data);
	}

	/**
	 * @api {get} /common_region/sub 查询子地区
	 * @apiGroup common_region
	 * @apiName sub
	 * @apiParam {Long} parRegionId
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/sub")
	@ResponseBody
	public CommonResult selectSub(CommonRegionDTO dto) {
		List<CommonRegionDTO> dtoList = commonRegionService.selectSubRegion(dto);
		return new CommonResult(HttpStatus.OK.toString(), "", dtoList);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public CommonResult deleteByPrimaryKey(CommonRegionDTO dto) throws Exception {
		CommonRegionDTO commonRegionDTO = new CommonRegionDTO();
		commonRegionDTO.setParRegionId(dto.getCommonRegionId());
		List<CommonRegionDTO> children = commonRegionService.selectByExample(commonRegionDTO);
		if (children != null && children.size() != 0) {
			return new CommonResult(HttpStatus.INTERNAL_SERVER_ERROR.toString(), "请先删除区域的下级区域");
		}

		commonRegionService.deleteByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}

}
